package queue;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 演示阻塞队列(LinkedBlockingQueueDemo)的基本用法
 */
public class LinkedBlockingQueueDemo {
    private static LinkedBlockingQueue<String> bq = new LinkedBlockingQueue<>();

    public static void main(String[] args) {
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("消费者线程开始从阻塞队列中获取数据......");
                try {
                    String data = bq.take();
                    System.out.println("消费者线程获取到的数据是:" + data);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        //启动消费者线程
        t.start();
        //让main线程暂时休眠5秒,让消费者线程先执行
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //让main线程充当生产者线程
        try {
            bq.put("balabala...");
            System.out.println("生产者线程开始向阻塞队列中添加了数据");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
